﻿/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */

using System.Linq;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    public abstract class RecordEventBaseInspector : EventBaseInspector
    {
        protected RecordTrackEventBase recordData;

        public void ShowRecord()
        {
            recordData = data as RecordTrackEventBase;
            ShowTransform();
            ShowRecordName();
            var currentFrame = ActionEditor.Instance.CurrentSelectFrameIndex;

            if (recordData.recordEventDatas.TryGetValue(currentFrame, out var recordEvent))
            {
                ShowMatrix4x4(recordEvent);
            }
            else
            {
                if (recordData.recordEventDatas.Count > 0)
                {
                    recordEvent = recordData.recordEventDatas.Values.First();
                    ShowMatrix4x4(recordEvent);
                }
            }

        }

        private void ShowRecordName()
        {
            var textField = new TextField("录制节点名称");
            textField.SetEnabled(false);
            textField.value = recordData.rocordTransformName;
            root.Add(textField);
        }

        private void ShowTransform()
        {
            var objField = new ObjectField("录制节点的引用");
            objField.objectType = typeof(Transform);

            if (recordData.recordTransform == null && ActionEditor.Instance.PreviewCharacterObj != null)
            {
                objField.value = ActionEditorUtility.FindChildTransformByNameIndex(
                    ActionEditor.Instance.PreviewCharacterObj.transform,
                    recordData.rocordTransformName);
            }
            else
            {
                objField.value = recordData.recordTransform;
            }

            objField.RegisterValueChangedCallback(p =>
            {
                recordData.recordTransform = p.newValue as Transform;

                if (p.newValue != null)
                {
                    recordData.rocordTransformName = p.newValue.name;
                    int index = 0;
                    foreach (Transform t in ActionEditor.Instance.PreviewCharacterObj.transform
                                 .GetComponentsInChildren<Transform>())
                    {
                        if (t.name == p.newValue.name)
                        {
                            index++;
                        }
                    }

                    if (index > 1)
                    {
                        ActionEditorLog.Error(ActionEditor.Instance.PreviewCharacterObj.name + " 对象 包含 " + index +
                                              " 个 名为： " + p.newValue.name + " 的对象，请保证该名的唯一，确保编辑器查找对象不出错!!");
                    }
                }
            });
            root.Add(objField);
        }

        private void ShowMatrix4x4(RecordEventData data)
        {
            Label label = new Label("录制节点相对于世界坐标的变换矩阵");
            var oneField = new Vector4Field();
            oneField.SetEnabled(false);
            var twoField = new Vector4Field();
            twoField.SetEnabled(false);
            var threeField = new Vector4Field();
            threeField.SetEnabled(false);
            var fourField = new Vector4Field();
            fourField.SetEnabled(false);

            var mat = data.localToRelativeRootWrold;
            oneField.value = new Vector4(mat.m00, mat.m01, mat.m02, mat.m03);
            twoField.value = new Vector4(mat.m10, mat.m11, mat.m12, mat.m13);
            threeField.value = new Vector4(mat.m20, mat.m21, mat.m22, mat.m23);
            fourField.value = new Vector4(mat.m30, mat.m31, mat.m32, mat.m33);

            root.Add(label);
            root.Add(oneField);
            root.Add(twoField);
            root.Add(threeField);
            root.Add(fourField);
        }

    }
}
